package pt.isec.a21210392.a21190220.ludotwist.GameLogic;

import java.io.Serializable;

import pt.isec.a21210392.a21190220.ludotwist.GameActivity;
import pt.isec.a21210392.a21190220.ludotwist.R;
import pt.isec.a21210392.a21190220.ludotwist.ToDraw;
import android.util.Log;

public class Pin implements Serializable
{
	private static final long serialVersionUID = 1L;
	private static final String[] colors = {"BLUE","YELLOW","GREEN","RED"};
	private static final int[] pinImages = {R.drawable.blue_pin,R.drawable.yellow_pin,R.drawable.green_pin,R.drawable.red_pin};
	private static final int[] startHousesImages = {R.drawable.blue_begining,R.drawable.yellow_begining,R.drawable.green_begining,R.drawable.red_begining};
	private static final int[] firstHouses = {1,18,35,52};
	private static final int[] preEntryHouses = {67,16,33,50};
	private static final int[] entryHouses = {68,76,84,92};
	private static final int[] finishHouses = {75,83,91,99};
	private static final int[] startHouses = {100,104,108,112};
	private static final int turnHouse = 67;
	protected String color;
	protected int pinNumber;
	private int pinImage;
	private int startHouseImage;
	private int actualHouse;
	private int firstHouse;
	private int preEntryHouse;
	private int entryHouse;
	private int finishHouse;
	private int firstStartHouse;
	private boolean rollAgain = false;
	protected boolean atTheBegin = true;
	private boolean finished = false;
	protected Player player;
	
	public Pin(Player player)
	{
		this.player = player;
		this.pinNumber = player.getNextPinNumber();		
		player.setPinNumber();
		int playerNumber = player.getNumber();
		this.color = colors[playerNumber];
		Log.d(color,pinNumber+"");
		this.pinImage = pinImages[playerNumber];
		this.startHouseImage = startHousesImages[playerNumber];
		this.firstHouse = firstHouses[playerNumber];
		this.preEntryHouse = preEntryHouses[playerNumber];
		this.entryHouse = entryHouses[playerNumber];
		this.finishHouse = finishHouses[playerNumber];
		this.firstStartHouse = startHouses[playerNumber];		
	}
	
	public void mergeWith(Pin pin)
	{
		GameActivity.boardView.removePinToDraw(new ToDraw(this));
		player.removePin(this);
		GameActivity.boardView.removePinToDraw(new ToDraw(pin));
		player.removePin(pin);
		player.addPin(new SuperPin(player, actualHouse));
	}
	public boolean shouldRollAgain()
	{
		if (rollAgain)
		{
			rollAgain = false;
			return true;
		}
		return false;
	}
	public boolean atTheBegin()
	{
		return atTheBegin;
	}
	public boolean finished()
	{
		return finished;
	}
	public void RollAgain()
	{
		rollAgain = true;
	}
	public void finish()
	{
		GameActivity.boardView.removePinToDraw(new ToDraw(this));
		player.finishedPin(this);
		finished = true;
	}
	public void putOut()
	{
		GameActivity.board.getHouse(actualHouse).removeVisitor();
		actualHouse = firstHouse;
		GameActivity.board.getHouse(actualHouse).addToBoardView(this);
		GameActivity.board.getHouse(actualHouse).arrivedHere(this, 0);		
		atTheBegin = false;
	}
	public void putIn()
	{
		int i=0;
		while (GameActivity.board.getHouse(firstStartHouse+i).getVisitor() != null)
		{
			i++;
		}
		actualHouse = firstStartHouse+i;
		GameActivity.board.getHouse(actualHouse).addToBoardView(this);
		GameActivity.board.getHouse(actualHouse).arrivedHere(this, 0);
		atTheBegin = true;
	}
	public int moveSimulation(int referenceHouse, int moves) //only for pins that are in board
	{
		int nextHouse = referenceHouse + moves; 
		
		if (referenceHouse <= preEntryHouse && nextHouse > preEntryHouse)
		{
			return entryHouse + (nextHouse - preEntryHouse) - 1;
		}
		if (referenceHouse <= turnHouse && nextHouse > turnHouse)
		{
			return nextHouse - turnHouse - 1;			
		}
		if (nextHouse > finishHouse)
		{
			return finishHouse - (nextHouse - finishHouse);
		}
		return nextHouse;
	}
	public void moveForward(int diceResult)
	{
		int forCycle = diceResult;
		GameActivity.board.getHouse(actualHouse).removeVisitor();
		while(actualHouse != finishHouse && forCycle > 0)
		{			
			actualHouse = moveSimulation(actualHouse,1);
			GameActivity.board.getHouse(actualHouse).addToBoardView(this);
			forCycle--;
		}
		while(forCycle > 0)
		{
			actualHouse = moveSimulation(actualHouse,-1);
			GameActivity.board.getHouse(actualHouse).addToBoardView(this);
			forCycle--;
		}
		GameActivity.board.getHouse(actualHouse).arrivedHere(this, diceResult);
	}
	public void moveBackward(int diceResult)
	{
		int forCycle = diceResult;
		GameActivity.board.getHouse(actualHouse).removeVisitor();
		while(forCycle > 0)
		{
			actualHouse = moveSimulation(actualHouse,-1);
			GameActivity.board.getHouse(actualHouse).addToBoardView(this);
			forCycle--;
		}
		GameActivity.board.getHouse(actualHouse).arrivedHere(this, diceResult);
	}
	public void jumpTo(int houseNumber)
	{
		GameActivity.board.getHouse(actualHouse).removeVisitor();
		setHouse(houseNumber);
	}
	public void setHouse(int houseNumber)
	{
		actualHouse = houseNumber;
		GameActivity.board.getHouse(actualHouse).addToBoardView(this);
		GameActivity.board.getHouse(actualHouse).arrivedHere(this, 0);		
	}
	public int getActualHouse()
	{
		return actualHouse;
	}
	public int getFirstHouse()
	{
		return firstHouse;
	}
	public int getFinishHouse()
	{
		return finishHouse;
	}
	public String getColor()
	{
		return color;
	}
	public int getStartHouseImage()
	{
		return startHouseImage;
	}	
	public int getImage()
	{
		return pinImage;
	}
	public int getPinNumber()
	{
		return pinNumber;
	}
	@Override
	public boolean equals(Object o) 
	{
		if (o instanceof Pin)
		{
			return this.color.equals(((Pin)o).getColor()) && this.pinNumber == ((Pin)o).getPinNumber();
		}
		return false;
	}
}
